package com.md.api.controller;

import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.HttpResult;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/coupon")
@RestController
public class CouponController {


	@Resource
	CouponService couponService;

	//我的优惠券
	@RequestMapping("/myCoupon")
	@ResponseBody
	public HttpResult myCoupon(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
		return couponService.myCoupon(request,qto);
	}


	//可使用的优惠券
	@RequestMapping("/availableCoupon")
	@ResponseBody
	public HttpResult availableCoupon(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (null==qto.getUseWay()){
			return HttpResult.PARAM_ERROR;
		}
		if (qto.getUseWay()==2&&CollectionUtils.isEmpty(qto.getSpuIds())){
			return HttpResult.PARAM_ERROR;
		}
		return couponService.availableCoupon(request,qto);
	}


	//可兑换的优惠券
	@RequestMapping("/redeemCoupon")
	@ResponseBody
	public HttpResult redeemCoupon(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
		return couponService.redeemCoupon(request,qto);
	}
	@NoRepeatSubmit
	@RequestMapping("/redeem")
	@ResponseBody
	public HttpResult redeem(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		return couponService.redeem(request,qto);
	}


}
